package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
    /*
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * */
    @GetMapping("/friends/{type}")
    public ResponseEntity userloves(@PathVariable("type") Integer type,
                                    @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr=null;
        if (type==1){
//            相互喜欢的
            pr=userInfoService.loveEach(page,pagesize);
        }
        if (type==2){
//            我喜欢的
            pr = userInfoService.userLoves(page, pagesize);

        }
        if (type==3){
//            喜欢我的(粉丝)
            pr = userInfoService.lovesUser(page, pagesize);

        }
        if (type==4){
//            谁看过我

        }
        return ResponseEntity.ok(pr);

    }
    @PostMapping("/fans/{uid}")
    public void modifyFansLoveStatus(@PathVariable("uid") Long uid){
        userInfoService.modifyFansLoveStatus(uid);


    }
    @DeleteMapping("/like/{uid}")
    public void deleteLoveStatus(@PathVariable("uid") Long uid){
        userInfoService.deleteLoveStatus(uid);


    }
    @GetMapping("/counts")
    public ResponseEntity findCounts(){
        CountsVo counts = userInfoService.findCounts();
//        System.out.println(counts.getFanCount());
//        System.out.println(counts.getLoveCount());
        return ResponseEntity.ok(counts);
    }



}
